package com.github.yoojia.flow;

import com.parkingwang.lang.data.Config;

/**
 * @author 陈小锅 (yoojiachen@gmail.com)
 * @since 1.0.0
 */
public abstract class InitialContext implements Initial {

    private final Config mInitConfig = new Config();
    private Context mContext;

    @Override
    public void onInit(Context context, Config configs) throws Exception {
        mContext = context;
        mInitConfig.copy(configs);
    }

    final protected Config getInitConfig() {
        return mInitConfig;
    }

    final protected Context getContext() {
        if (mContext == null) {
            throw new IllegalStateException("Method<OnInit> MUST CALL super");
        }
        return mContext;
    }

    final protected Event createEvent(byte[] payload, String topic, String sourceAddress, String destAddress) {
        return Event.create(getContext().nextSequenceId(),
                payload,
                topic,
                sourceAddress,
                destAddress,
                System.currentTimeMillis());
    }

    final protected Event createEvent(long sequence, byte[] payload, String topic, String sourceAddress, String destAddress) {
        return Event.create(
                sequence,
                payload,
                topic,
                sourceAddress,
                destAddress,
                System.currentTimeMillis());
    }

}
